package tongcheng.com.tongcheng.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseViewHolder;
import tongcheng.com.tongcheng.widght.PhotoPopWindow;

/**
 * Created by P on 2017/7/18.
 */

public class SendOrderAdapter extends RecyclerView.Adapter<SendOrderAdapter.SendOrderViewholder> {
    public SendOrderAdapter() {

    }

    public List<String> filepath1;
    public Context mContext;
    public Activity mActivity;
    public RelativeLayout mView;
    public SendOrderAdapter(List<String> filepath, Context context, Activity activity,RelativeLayout view) {
        this.filepath1 = filepath;
        this.mContext = context;
        this.mActivity = activity;
        this.mView = view;
    }

    public void setFilepath1(List<String> filepath1) {
        this.filepath1 = filepath1;
    }

    @Override
    public SendOrderViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_sendorder, null);
        return new SendOrderViewholder(view);
    }

    @Override
    public void onBindViewHolder(final SendOrderViewholder holder, final int position) {
        if (filepath1 != null) {
            if (0 == filepath1.size()) {
                holder.item_sendorder_add.setBackgroundResource(R.mipmap.zhaopian_zhenjia);
                holder.tv_shanChu.setVisibility(View.GONE);
//                holder.item_sendorder_add.setImageResource(R.mipmap.tianjia);
                holder.item_sendorder_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        takePhoto();
                    }
                });

            } else  {

                if (filepath1.size() == position) {
//                    holder.tv_shanChu.setVisibility(View.INVISIBLE);
                    holder.item_sendorder_add.setImageResource(R.mipmap.zhaopian_zhenjia);
                    holder.item_sendorder_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            takePhoto();
                        }
                    });
                } else {

//                    holder.tv_shanChu.setVisibility(View.VISIBLE);
//                    holder.tv_shanChu.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            ShowToast.showToast("删除照片");
//                            filepath.remove(position);
//                            filename.remove(position);
//                            mSendOrderAdapter.setFilepath1(filepath);
//                            mSendOrderAdapter.notifyDataSetChanged();
//
//                        }
//                    });

                    holder.item_sendorder_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

//                            ShowToast.showToast("我被点了");
                        }
                    });


                    holder.item_sendorder_add.setBackgroundResource(android.R.color.transparent);
//                    holder.item_sendorder_add.setBackgroundResource(R.mipmap.zhaopian_zhenjia);
                    Glide.with(mContext).load(filepath1.get(position))
                                .into(holder.item_sendorder_add);


                }
            }
        }

    }

    private void takePhoto() {
        PhotoPopWindow PhotoPopWindow = new PhotoPopWindow(mContext, mActivity);
        PhotoPopWindow.maxChooseNum = 4;
        PhotoPopWindow.show(mView);
    }

    @Override
    public int getItemCount() {

        return filepath1==null?1:filepath1.size()==4?4:filepath1.size()+1;
    }

    public  class SendOrderViewholder extends BaseViewHolder{
        public ImageButton item_sendorder_add;
        public TextView tv_shanChu;
        public SendOrderViewholder(View itemView) {
            super(itemView);
        }

        @Override
        public void init(View itemview) {
            item_sendorder_add = itemview.findViewById(R.id.item_sendorder_add);
            tv_shanChu = itemview.findViewById(R.id.tv_shanChu);
        }
    }



}



